require("../css/login.less")

window.onload = function () {
    let btn = document.querySelector(".btn")
    let account = document.querySelector("#account")
    let pwd = document.querySelector("#pwd")
  
    
    // 非空验证
    btn.addEventListener("click", function () {
        if (!(account.value && pwd.value)) {0
            tool.toast(0,"账号或者密码不能为空!")
        } else {
            // 手机正则验证
            let reg = /^1[3-9][0-9]{9}$/
            if (!reg.test(account.value)) {
                tool.toast(0,"请输入正确的手机号")
            } else {
                let data = {
                    account: account.value,
                    password: pwd.value
                }
                
                // 发送ajax请求
                http.post("/users/login",data,function(res){
                    if (res.status===0) {
                        // 登录成功就跳转页面
                        // 跳转之前先存储数据
                        localStorage.setItem("userId",res.data.user.userId)
                        tool.toast(1,"正在跳转首页,请稍后......")
                        location.href = "index.html"
                    } else {
                        // 登录不成功就把后端提醒直接返回给用户
                        tool.toast(0,res.msg)
                    }
                })
            }
        }
    })





}